/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucmconfchecker.pucm2graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import ucmconfchecker.conformance.Scenario;
import ucmconfchecker.pArchitecture.Responsibility;
import ucmconfchecker.pUCM.*;

/**
 *
 * @author Administrador
 */
public class Ucm2Graph{
    
    public Scenario asGraph(UCM ucm, String name){
        if(ucm!=null){
            Scenario<Responsibility> graph = new Scenario<Responsibility>(name);
            List<UCMNode> nodes = ucm.getNodes();
            if(nodes!=null){
                for(UCMNode node: nodes){
                    if(node.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                        graph.addVertex(((UCMResponsibility)node).getResponsibility());
                    }
                }
            }
            HashMap<UCMNode, ArrayList<UCMNode>> arcs= ucm.getArcs();
            if(arcs!=null &&arcs.keySet()!=null){
               for(UCMNode source: arcs.keySet()){
                   Responsibility respSource =null;
                   if(source.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                      //Obtiene la Responsabilidad
                      respSource=(((UCMResponsibility)source).getResponsibility());
                   }
                   if(respSource!=null){
                        //Será solo un arco, porque es responsabilidad
                        for(UCMNode target: arcs.get(source)){
                            Responsibility respTarget =null;
                            if(target.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                                respTarget= (((UCMResponsibility)target).getResponsibility());
                                graph.addEdge(respSource,respTarget);
                            }
                            if(target.getType().equals(UCMNode.Type.JOIN)){
                                //toma como destino la responsabilidad siguiente al join
                                //debe ser solo 1.
                                UCMJoin join = (UCMJoin)target;
                                //TODO evaluar si es JOIN AND / OR
                                //MultiType type = join.getCondition==AND? Scenario.MultiType.AND: Scenario.MultiType.OR
                                graph.setPostconditionsType(respSource,Scenario.MultiType.AND);
                                for(UCMNode newTarget:arcs.get(target)){
                                    if(source.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                                        //Obtiene la Responsabilidad
                                        respTarget=(((UCMResponsibility)newTarget).getResponsibility());
                                    }
                                }//toma la ultima.
                                graph.addEdge(respSource,respTarget);
                            }
                            if(target.getType().equals(UCMNode.Type.FORK)){
                                //Toma como destino las responsabilidades siguientes al fork
                                UCMJoin fork = (UCMJoin)target;
                                 //MultiType type = fork.getCondition==AND? Scenario.MultiType.AND: Scenario.MultiType.OR
                                graph.setPostconditionsType(respSource,Scenario.MultiType.AND);
                                for(UCMNode newTarget:arcs.get(target)){
                                    if(source.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                                        //Obtiene la Responsabilidad
                                        respTarget=(((UCMResponsibility)newTarget).getResponsibility());
                                        graph.addEdge(respSource,respTarget);
                                    }
                                }
                            }
                        }
                   }
               }
            }
            HashMap<UCMNode, ArrayList<UCMNode>> preconditions = ucm.getPreconditions();
            System.out.println("PRECONDITIONS!: "+preconditions);
            if(preconditions!=null){
                for(UCMNode target: preconditions.keySet()){
                    if(target.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                        //Obtiene la Responsabilidad
                        Responsibility respTarget = ((UCMResponsibility)target).getResponsibility();
                        for(UCMNode source: preconditions.get(target)){
                            if(source.getType().equals(UCMNode.Type.RESPONSIBILITY)){
                                //Obtiene la Responsabilidad
                                Responsibility respSource = ((UCMResponsibility)source).getResponsibility();
                                graph.addEdge(respSource,respTarget);
                            }
                        }
                    }
                }
            }
            System.out.println("Graph:");
            System.out.println(">vertex: "+graph.getVertexSet());
            System.out.println(">edges: "+graph.getEdgeSet());
            return graph;
        }else{
            return null;
        }   
    }
}
